﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Data;
namespace Common
{
    public class ExportToExcel
    {
        public static int Export(DataGridView dgv)
        {
            int relVal = 0;
            DataTable dt = dgv.DataSource as DataTable;
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Execl files (*.xls)|*.xls";
            dlg.FilterIndex = 0;
            dlg.RestoreDirectory = true;
            dlg.CreatePrompt = true;
            dlg.Title = "保存为Excel文件";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Stream myStream;
                myStream = dlg.OpenFile();
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));//.GetEncoding(-0));
                string columnTitle = "";
                try
                {
                    //DataTable table = BearDataOpr.SelectFromSql(sql);
                    DataTable table = dt.Copy();
                    //DataTable dt = table.Copy();

                    //写入列标题     
                    for (int i = 0; i < dgv.ColumnCount; i++)
                    {
                        columnTitle += dgv.Columns[i].HeaderText;
                        columnTitle += "\t";
                    }
                    sw.WriteLine(columnTitle);
                    DateTime dtValue;
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        for (int j = 0; j < table.Columns.Count; j++)
                        {

                            if (dt.Rows[i][j] != DBNull.Value && table.Columns[j].Caption.ToString().Contains("时间"))
                            {
                                if (dt.Rows[i][j] != null && DateTime.TryParse(dt.Rows[i][j].ToString(), out dtValue))
                                    dt.Rows[i][j] = Convert.ToDateTime(dt.Rows[i][j]).ToString("yyyy-MM-dd HH:mm:ss");
                                else
                                    dt.Rows[i][j] = "";
                            }

                        }
                    }


                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        string columnValue = "";
                        for (int k = 0; k < dt.Columns.Count; k++)
                        {
                            columnValue += dt.Rows[j][k].ToString().Trim();
                            columnValue += "\t";
                        }
                        sw.WriteLine(columnValue);
                    }
                    sw.Close();
                    myStream.Close();

                    relVal = 1;

                }
                catch (Exception ex)
                {
                    MessageBox.Show("保存EXCEL失败，详细原因查看日志(Log)文件");
                    lLogWrite.WriteLog("保存EXCEL失败" + ex.ToString());
                    relVal = -1;
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }

            }
            return relVal;
        }
    }
}
